package com.lrkj.back.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.Const;
import com.lrkj.back.entity.TbDepartment;
import com.lrkj.back.entity.TbEmployee;
import com.lrkj.back.entity.TbPackageMuban;
import com.lrkj.back.entity.ZsShenqing;
import com.lrkj.back.entity.ZsShenqingShoushubaoMingxi;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lrkj.back.service.TbDepartmentService;
import com.lrkj.back.service.TbManufactorService;
import com.lrkj.back.service.TbPackageMubanService;
import com.lrkj.back.service.TbSeqService;
import com.lrkj.back.service.ZsShenqingService;
import com.lrkj.back.service.ZsShenqingShoushubaoMingxiService;
import com.lrkj.back.service.ZsShenqingYicipinMingxiService;
import com.lrkj.back.service.ZsYicipinKucunService;
import com.lrkj.back.service.ZsYicipinRukuService;
import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/shoushubao")
public class ZsShoushubaoController extends BaseController{
	
	@Autowired
	private ZsYicipinRukuService yicipinService;
	@Autowired
	private ZsShenqingService shenqingService;
	@Autowired
	private TbManufactorService manufactorService;
	@Autowired
	private ZsShenqingYicipinMingxiService sqmxService;
	
	@Autowired
	private TbSeqService seqService;
	@Autowired
	private ZsShenqingShoushubaoMingxiService ssbservice;
	@Autowired
	private ZsYicipinKucunService kucunService;
	@Autowired
	private TbPackageMubanService mubanservice;
	
	@Autowired
	private TbDepartmentService  tbDepartmentServices;
	
	//临床科室--包使用记录   点击查询
	@RequestMapping("/shoushubaoList.do")
	public ModelAndView yicipinApplyList(String searchTxt,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<>();
		
		//显示下拉
		List<TbDepartment> keshilist = tbDepartmentServices.findAllDepartment();
		request.setAttribute("keshilist", keshilist);
		if(StringUtils.isNotBlank(searchTxt)){
			searchTxt=searchTxt.trim();
			map.put("shenqingKeshi", searchTxt);
		}
		//只看手术包
		map.put("shenqingFenlei", 0);
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		//把分页信息传过去
		
		PageInfo<ZsShenqing> page = shenqingService.findAllShenqing(map);
		request.setAttribute("shenqingList", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		//返回到页面
		return new ModelAndView("/linchuang/shoushubaoshenqing/shoushubao_list");
	}
	//点添加时用到的方法，把库存信息传到下拉框
	@RequestMapping("/shoushubaoApplyEdit.do")
	public ModelAndView yicipinApplyEdit(String id, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		ZsShenqing shenqing = (id == null) ? new ZsShenqing() : shenqingService.selectByPrimaryKey(id);
//		TbEmployee emp =(TbEmployee)request.getSession().getAttribute("emp");
//		Integer departId = null;
//		if (emp != null) {
//			departId = emp.getDepartmentId();
//		}
//		Map<String,Object> map =new HashMap<String,Object>();
//		if(StringUtils.isNotBlank(id)){
//			map.put("shenqingKeshi", id);
//		}
//		List<ZsShenqingYicipinMingxi> mingxiList = shenqingservice.findAllShenqingMingxi(map);
//		if(mingxiList == null)
//			mingxiList = new ArrayList<ZsShenqingYicipinMingxi>();
//		shenqing.setMingxiList(mingxiList);
        
		Map<String, Object> map = new HashMap<>();
		map.put("packageCategory", 1);
//		map.put("packageDepartid", departId);
		List<TbPackageMuban> proList = mubanservice.findAllPackageNoPageList(map);
		request.setAttribute("proList", proList);
		request.setAttribute("proJson", JSON.toJSON(proList));
		request.setAttribute("shenqing", shenqing);
		return new ModelAndView("/linchuang/shoushubaoshenqing/shoushubao_edit");
	}
	//点保存按纽时用到的方法
	@RequestMapping("/shoushubaoApplySave.do")
	@ResponseBody
	@Transactional
	public Map<Object, Object> yicipinApplySave(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		//保存时先保存主表，再保存手术包那个表
		try {
			String[] proId = request.getParameterValues("productId");
			String[] proName = request.getParameterValues("productName");
			String[] proNum = request.getParameterValues("productNum");
			SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
			String shenqingliushui = sdf.format(new Date())+seqService.seqVal("shenqingno");
			ZsShenqing sq = new ZsShenqing();
			TbEmployee emp =(TbEmployee)request.getSession().getAttribute("emp");
			sq.setShenqingKeshi(emp.getDepartmentId().toString());
//			sq.setShenqingShenherenTiaoma(emp.getEmployeeNo());
			sq.setShenqingShenqingrenTiaoma(emp.getEmployeeNo());
			sq.setShenqingLiushui(shenqingliushui);
			sq.setRecordTime(new Date());
			sq.setShenqingFenlei(0);
			sq.setShenqingTime(new Date());
			sq.setShifouXuyao(0);
			//默认审核状态0
			sq.setShenqingShenheZhuangtai(0);
			shenqingService.insert(sq);
			ZsShenqingShoushubaoMingxi mingxi = null;
			//同时加入多 个对象
			for(int i = 0 ;i<proId.length;i++){
				mingxi = new ZsShenqingShoushubaoMingxi();
				mingxi.setShenqingLiushui((shenqingliushui));
				mingxi.setShenqingMingxiLiushui(sdf.format(new Date())+seqService.seqVal("shoushubaono"));
				mingxi.setShenqingWupinbaoMuban(proId[i]);
				mingxi.setShenqingWupinbaoMingcheng(proName[i]);
				mingxi.setShenqingWupinbaoShuliang(Integer.valueOf(proNum[i]));
				mingxi.setFafangZhuangtai(0);
				mingxi.setShenqingKeshi(emp.getDepartmentId().toString());
				mingxi.setRecordTime(new Date());
//				mingxi.setShenqingKeshi(emp.getDepartment().getDepartmentName());
				ssbservice.insert(mingxi); 
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	//点删除时用到的方法
	
	@RequestMapping("/yicipinApplyDel.do")
	@ResponseBody
	@Transactional
	public Map<Object, Object> yicipinApplyDel(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			for(String tid : arraId){
				shenqingService.deleteByPrimaryKey(tid);
				Map<String,Object> map =new HashMap<String,Object>();
				if(StringUtils.isNotBlank(tid)){
					map.put("shenqingLiushui", tid);
				}
				List<ZsShenqingShoushubaoMingxi> mingxiList = ssbservice.findAllShoushubaoApplyDetail(map);
				System.out.println(mingxiList.size());
				for(ZsShenqingShoushubaoMingxi mx:mingxiList) {
					System.out.println(mx.getShenqingMingxiLiushui());
					ssbservice.deleteByPrimaryKey(mx.getShenqingMingxiLiushui());
				}
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	//右边显示详细信息的方法
	@RequestMapping("/shoushubaoApplyDetailList.do")
	@ResponseBody
	public Map<Object, Object> yicipinApplyDetailList(String id,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			Map<String,Object> map =new HashMap<String,Object>();
			if(StringUtils.isNotBlank(id)){
				map.put("shenqingLiushui", id);
			}
			List<ZsShenqingShoushubaoMingxi> mingxiList = ssbservice.findAllShoushubaoApplyDetail(map);
			rtnMap.put("mxList", mingxiList);
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
}